﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.LinkManage
{
	public partial class LinkClassNavigation : BasePage
	{
		#region property
		/// <summary>
		/// 链接的地址。
		/// </summary>
		protected string LinkUrl
		{
			get
			{
				return Request["LinkUrl"];
			}
		}

		/// <summary>
		/// 站点标识的字段。
		/// </summary>
		protected string ClassIDQueryStringField
		{
			get
			{
				return Request["ClassIDQueryStringField"];
			}
		}

		/// <summary>
		/// 链接的目标。
		/// </summary>
		protected string LinkTarget
		{
			get
			{
				string linkTarget = Request["LinkTarget"];
				return string.IsNullOrEmpty(linkTarget) ? "ContentFrame" : linkTarget;
			}
		}

		/// <summary>
		/// 选中的节点。
		/// </summary>
		protected int? SelectedClassID
		{
			get
			{
				string selectedClassID = Request["SelectedClassID"];
				if (string.IsNullOrEmpty(selectedClassID))
					return null;
				else
					return Convert.ToInt32(selectedClassID);
			}
		}
		#endregion

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				this.DataBind();
				BindSiteData();
				InitSelectedClass();
			}
		}

		/// <summary>
		/// 初始化选择类别。
		/// </summary>
		protected void InitSelectedClass()
		{
			LinkClass pathNode = null;
			if (SelectedClassID.HasValue)
				pathNode = LinkClasses.GetItem(SelectedClassID.Value, true);

			if (pathNode != null)
			{
				TreeNode node = ExpandNode(pathNode);
				if (node != null) node.Selected = true;
			}
		}

		protected TreeNode ExpandNode(LinkClass pathNode)
		{
			TreeNode node;
			if (pathNode.Parent == null)
			{
				node = FindNode(LinkClassView.Nodes, pathNode.SiteId.ToString());
				if (node != null)
					node.Expand();

				node = FindNode(node.ChildNodes, pathNode.ClassId.ToString());
				return node;
			}

			node = ExpandNode(pathNode.Parent);
			if (node == null) return null;
			node.Expand();

			node = FindNode(node.ChildNodes, pathNode.ClassId.ToString());
			return node;
		}

		/// <summary>
		/// 查找具有指定值的节点。
		/// </summary>
		/// <param name="nodes">节点集合。</param>
		/// <param name="value">查找的节点值。</param>
		/// <returns>找到的节点。</returns>
		protected TreeNode FindNode(TreeNodeCollection nodes, string value)
		{
			foreach (TreeNode node in nodes)
			{
				if (node.Value == value)
					return node;
			}
			return null;
		}

		/// <summary>
		/// 绑定站点数据。
		/// </summary>
		protected void BindSiteData()
		{
			List<Site> sites = Sites.GetItems();

			foreach (Module.Site site in sites)
			{                
				// 权限验证
				if (!Links.GetRootManageListRight(site.SiteId))
					continue;

				TreeNode node = new TreeNode();
				node.Text = site.SiteName;
				node.Value = site.SiteId.ToString();
				node.SelectAction = TreeNodeSelectAction.None;
				node.PopulateOnDemand = true;

				LinkClassView.Nodes.Add(node);
			}
		}

		/// <summary>
		/// 绑定链接类别数据。
		/// </summary>
		/// <param name="parentNode">父级节点。</param>
		protected virtual void BindLinkClassData(TreeNode parentNode)
		{
			List<LinkClass> childClasses;

			if (parentNode.Depth == 0)
				childClasses = LinkClasses.GetRoots(int.Parse(parentNode.Value));
			else
				childClasses = LinkClasses.GetChildren(int.Parse(parentNode.Value));

			foreach (LinkClass childClass in childClasses)
			{
				if (childClass.IsHidden) continue;

				if (!Links.GetManageListRight(childClass.ClassId, childClass.SiteId))
					continue;

				TreeNode node = new TreeNode();
				node.Text = childClass.ClassName;
				node.Value = childClass.ClassId.ToString();

				if (childClass.ChildCount > 0)
				{
					node.SelectAction = TreeNodeSelectAction.None;
					node.PopulateOnDemand = true;
				}
				else
				{
					node.SelectAction = TreeNodeSelectAction.Select;
					node.NavigateUrl = LinkUrl + "?" + ClassIDQueryStringField + "=" + childClass.ClassId + string.Format("&SiteId={0}", childClass.SiteId);
					node.Target = LinkTarget;
					node.PopulateOnDemand = false;
					node.ToolTip = childClass.Memo;
				}

				parentNode.ChildNodes.Add(node);
			}
		}

		protected void LinkClassView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
		{
			BindLinkClassData(e.Node);
		}
	}
}
